package com.hz.ruoyistorage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hz.ruoyistorage.domain.LgSubCold;
import com.hz.ruoyistorage.domain.mypojo.ColdType;
import com.hz.ruoyistorage.domain.mypojo.LgSubColdSome;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author gj
 * @Date 2025/3/6 9:52
 * @注释
 */
public interface LgSubColdMapper extends BaseMapper<LgSubCold> {
    //获得小冷库类型与价格对照表
    @Select("select cold_type_id,type_name,price_meter from lg_cold_type")
    public List<ColdType> getColdType();

    //获得单价
    @Select("select price_meter from lg_cold_type where cold_type_id = #{coldTypeId}")
    public String getPriceMeter(Integer coldTypeId);

    //根据id查询冷库
    @Select("select * from lg_sub_cold where sub_id = #{subId}")
    public LgSubCold selectById(Integer subId);

    //根据id修改冷库
    @Update({
            "<script>",
            "UPDATE lg_sub_cold",
            "<set>",
            "<if test='parentId != null'>parent_id = #{parentId},</if>",
            "<if test='subName != null'>sub_name = #{subName},</if>",
            "<if test='description != null'>description = #{description},</if>",
            "<if test='capacity != null'>capacity = #{capacity},</if>",
            "<if test='dailyPrice != null'>daily_price = #{dailyPrice},</if>",
            "<if test='coldId != null'>cold_id = #{coldId},</if>",
            "<if test='managerId != null'>manager_id = #{managerId},</if>",
            "<if test='status != null'>status = #{status},</if>",
            "<if test='currentOwner != null'>current_owner = #{currentOwner},</if>",
            "<if test='createBy != null'>create_by = #{createBy},</if>",
            "<if test='updateBy != null'>update_by = #{updateBy},</if>",
            "<if test='createTime != null'>create_time = #{createTime},</if>",
            "<if test='updateTime != null'>update_time = #{updateTime},</if>",
            "<if test='deleteStatus != null'>delete_status = #{deleteStatus},</if>",
            "</set>",
            "WHERE sub_id = #{subId}",
            "</script>"
    })
    public int updateById(LgSubCold lgSubCold);

    //根据id修改每日价格
    @Update({
            "<script>",
            "UPDATE lg_sub_cold",
            "<set>",
            "<if test='dailyPrice != null'>daily_price = #{dailyPrice},</if>",
            "</set>",
            "WHERE sub_id = #{subId}",
            "</script>"
    })
    public int updateDailyPriceById(LgSubCold lgSubCold);
    //获得id,name,parentId
    @Select("select sub_id,sub_name,parent_id from lg_sub_cold")
    public List<LgSubColdSome> getIdAndName();



    @Select("SELECT b.cold_id, b.cold_name, a.sub_id, a.sub_name, a.capacity,a.daily_price, c.cold_type_id, c.type_name " +
            "FROM lg_sub_cold AS a " +
            "LEFT JOIN lg_cold_storage AS b ON a.parent_id = b.cold_id " +
            "LEFT JOIN lg_cold_type AS c ON a.cold_id = c.cold_type_id")
    public List<Map<String,Object>> getSubColdSome();

    @Update("UPDATE lg_sub_cold SET status = #{status} WHERE sub_id = #{subId}")
    public void updateStatus(@Param("subId") Integer subId,
                             @Param("status")  Integer status);

}
